package com.chenyaowei.happyhub.controller;

import com.chenyaowei.happyhub.mapper.AdminMapper;
import com.chenyaowei.happyhub.pojo.Admin;
import com.chenyaowei.happyhub.pojo.GoodsPojo.Good;
import com.chenyaowei.happyhub.service.GoodsService.GoodService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminLoginController {
    @Resource
    AdminMapper adminMapper;
    @Resource
    JavaMailSenderImpl mailSender;

    @RequestMapping("/login")
    String userLogin(Model model) throws JsonProcessingException {
            return "login";
    }
    @RequestMapping("/login/tofindpassword")
    String tofindpassword(){
        return "findpassword";
    }

    @RequestMapping("/login/findpassword")
    String findPassword(Model model,String username,String email){
        Admin user=adminMapper.selectAdminByUsername(username);
        if (user==null||user.equals("")||email==null||email.equals("")) {
            if (username!=null||!username.equals(""))
                model.addAttribute("error", "用户名不存在");
            if(email==null||email.equals(""))
                model.addAttribute("error", "邮箱与用户名不符");
            return "findpassword";
        }
        else{
            if(!user.getEmail().equals(email)){
                model.addAttribute("error", "邮箱与用户名不符");
                System.out.println("邮箱与用户名不符");
                return "findpassword";
            }
            SimpleMailMessage mailMessage=new SimpleMailMessage();
            mailMessage.setText("来自HappyHub的邮件 -> 用户名: "+ user.getUsername() + "    密码: "+ user.getPassword());
            mailMessage.setTo(email);
            mailMessage.setFrom("2829235190@qq.com");
            model.addAttribute("message","密码已发送至邮箱");
            mailSender.send(mailMessage);
            return "login";
        }
    }
}
